﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace XChipUtils
{
    public class UtilsHelper : IUtilsHelper
    {
        /// <summary>
        /// 获取指定父元素下的所有 TextBox 控件
        /// </summary>
        /// <param name="parent">父元素</param>
        /// <returns>TextBox 控件列表</returns>
        public List<TextBox> GetAllTextBoxes(DependencyObject parent)
        {
            List<TextBox> textBoxes = new List<TextBox>();

            if (parent == null) return textBoxes;

            // 遍历子控件
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);

                // 如果子控件是 TextBox，则添加到列表中
                if (child is TextBox textBox)
                {
                    textBoxes.Add(textBox);
                }

                // 递归调用以查找子控件的子控件
                textBoxes.AddRange(GetAllTextBoxes(child));
            }

            return textBoxes;
        }

        /// <summary>
        /// 通过名称查找 TextBox（不区分大小写）
        /// </summary>
        /// <param name="parent">父元素</param>
        /// <param name="name">要查找的 TextBox 名称</param>
        /// <returns>找到的 TextBox 或 null</returns>
        public TextBox FindTextBoxByNameIgnoreCase(DependencyObject parent, string name)
        {
            if (parent == null || string.IsNullOrEmpty(name)) return null;

            // 检查当前对象是否为 TextBox 且名称匹配（不区分大小写）
            if (parent is TextBox textBox && textBox.Name?.Equals(name, StringComparison.OrdinalIgnoreCase) == true)
            {
                return textBox;
            }

            // 遍历子元素
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                var result = FindTextBoxByNameIgnoreCase(child, name);
                if (result != null)
                {
                    return result;
                }
            }

            // 未找到匹配的 TextBox
            return null;
        }

        /// <summary>
        /// 获取指定父元素下的所有指定类型的控件
        /// </summary>
        /// <typeparam name="T">控件类型</typeparam>
        /// <param name="parent">父元素</param>
        /// <returns>控件列表</returns>
        public List<T> GetVisualChildren<T>(DependencyObject parent) where T : DependencyObject
        {
            List<T> children = new List<T>();

            if (parent == null) return children;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);

                if (child is T tChild)
                {
                    children.Add(tChild);
                }

                children.AddRange(GetVisualChildren<T>(child));
            }

            return children;
        }

        /// <summary>
        /// 通过名称查找指定类型的控件（不区分大小写）
        /// </summary>
        /// <typeparam name="T">控件类型</typeparam>
        /// <param name="parent">父元素</param>
        /// <param name="name">要查找的控件名称</param>
        /// <returns>找到的控件或 null</returns>
        public T FindVisualChildByNameIgnoreCase<T>(DependencyObject parent, string name) where T : FrameworkElement
        {
            if (parent == null || string.IsNullOrEmpty(name)) return null;

            // 检查当前对象是否为指定类型且名称匹配（不区分大小写）
            if (parent is T element && element.Name?.Equals(name, StringComparison.OrdinalIgnoreCase) == true)
            {
                return element;
            }

            // 遍历子元素
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                var result = FindVisualChildByNameIgnoreCase<T>(child, name);
                if (result != null)
                {
                    return result;
                }
            }

            // 未找到匹配的控件
            return null;
        }

        /// <summary>
        /// 查找父元素中的指定类型的父控件
        /// </summary>
        /// <typeparam name="T">父控件类型</typeparam>
        /// <param name="child">子元素</param>
        /// <returns>找到的父控件或 null</returns>
        public T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
        {
            if (child == null) return null;

            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            if (parentObject == null) return null;

            if (parentObject is T parent)
            {
                return parent;
            }

            return FindVisualParent<T>(parentObject);
        }
    }


    #region 代码调用
    //// 创建实例
    //IUtilsHelper utilsHelper = new UtilsHelper();

    //// 获取所有 TextBox
    //var allTextBoxes = utilsHelper.GetAllTextBoxes(this);

    //// 通过名称查找 TextBox
    //var specificTextBox = utilsHelper.FindTextBoxByNameIgnoreCase(this, "txtUserName");

    //// 获取所有按钮
    //var allButtons = utilsHelper.GetVisualChildren<Button>(this);

    //// 通过名称查找按钮
    //var specificButton = utilsHelper.FindVisualChildByNameIgnoreCase<Button>(this, "btnSubmit");

    //// 查找父容器
    //var parentGrid = utilsHelper.FindVisualParent<Grid>(specificTextBox);
    #endregion
}
